using Bowling.Constants;
using Bowling.Enums;

namespace Bowling;

internal class LastFrame : Frame
{
    internal int? ThirdPins => PinsRecord.ElementAtOrDefault(2);

    internal override FrameProgress Progress
    {
        get
        {
            var frame = new RegularFrame(); 
            frame.PinsRecord.Add(FirstPins);
            frame.PinsRecord.Add(SecondPins);

            if (frame.Progress == FrameProgress.NotStarted)
                return FrameProgress.NotStarted;
            else if (frame.Progress == FrameProgress.InProgress)
                return FrameProgress.InProgress;
            else if (frame.Type == FrameType.Strike || frame.Type == FrameType.Spare)
                return ThirdPins is null ? FrameProgress.InProgress : FrameProgress.Completed;
            else
                return FrameProgress.Completed;
        }
    }

    public override string Visualize()
    {
        List<string> grids = new List<string>();

        for (int i = 0; i < 3; i++)
        {
            int? pins = PinsRecord.ElementAtOrDefault(i);
            string grid = pins switch
            {
                BowlingConstant.AllPins => "x",
                null => "",
                _ => $"{pins}",
            };

            grids.Add(grid);
        }

        if (FirstPins != BowlingConstant.AllPins && (FirstPins + SecondPins) == BowlingConstant.AllPins)
        {
            grids[1] = "/";
        }

        if (SecondPins != BowlingConstant.AllPins && (SecondPins + ThirdPins) == BowlingConstant.AllPins)
        {
            grids[2] = "/";
        }

        return $"[{string.Join(',', grids)}]";
    }
}